<?php
	include ('../config/conexao.php');
	include ('../config/validacao.php');
	include ('../modlogin/logger.php');

	if(isset($_GET["id"]) ){
		$rs = mysql_query('SELECT idramais FROM `ramais` WHERE `setor_idsetor` ='.$_GET["id"]);
		$string = '';
		// loop through each zipcode returned and format the response for jQuery
		if ( $rs && mysql_num_rows($rs) )
		{
				while( $row = mysql_fetch_array($rs) )
				{
						$string = $string.$row['idramais']."|";

				}
				echo "<script type='text/javascript' language='Javascript'>
				var resposta = confirm('Existem ramais cadastrados neste setor. Altera-los de setor?');
				if (resposta == true) {
						window.location.href = '/grambel/c_ramais/editar_ramais.php?string=".$string."';
				}else{
						alert('Setor nao excluido!')
						window.location.href = 'e_setor.php';
				}
				</script>";

		}else{
				$sql = "DELETE FROM setor WHERE idsetor='".$_GET['id']."';";
				mysql_query("SET NAMES 'utf8'");
				$retval = mysql_query($sql,$link );
				if(! $retval )
				{
						Logger('Erro ao excluir setor:'.mysql_error());
						echo "<script>alert('Erro ao excluir setor: ". mysql_error()."');</script>";
						//echo $sql;
						echo "<script>window.location.href = 'e_setor.php'";
				}
				else{
						Logger($sql,99);
						echo "<script>alert('Setor excluido!');</script>";
						echo "<script>window.location.href = 'e_setor.php';</script>";
				}
		}
	}
?>
